package com.sgb.kjwl.viewmodel.home;

import android.util.Log;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.enterprise.CompanyListEntity;
import com.sgb.kjwl.model.entity.enterprise.CopyrightListEntity2;
import com.sgb.kjwl.model.entity.enterprise.EpHotIpEntity;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.sgb.kjwl.model.entity.enterprise.IntellpropEntity;
import com.sgb.kjwl.model.entity.enterprise.NeedEntity;
import com.sgb.kjwl.model.entity.enterprise.PatentEntity;
import com.sgb.kjwl.model.entity.enterprise.TeamChangeEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.fragment.home.EnterpriseItemFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EnterpriseItemFragModel extends BaseViewModel {
    private EnterpriseItemFragment mFragment;
    private int pageCompany = 1;
    private int pageProperty = 1;


    public EnterpriseItemFragModel(EnterpriseItemFragment fragment) {
        mFragment = fragment;
    }

    /**
     * 商标
     */
    public void getTrademarkList(int page) {
        //显示缓存
//        Gson gson = new Gson();
//        //先显示缓存
//        ListResponse<List<IntellpropEntity>> list;
//        String cacheCachegoryModel = MkvUtils.get().getString(MkvUtils.CacheModular, CacheMarker.ServiceListData, "");
//        if (!cacheCachegoryModel.isEmpty()) {
//            Type type = new TypeToken<ListResponse<List<IntellpropEntity>>>() {
//            }.getType();
//            list = gson.fromJson(cacheCachegoryModel, type);
//            mFragment.showTrademarkList(list);
//        }
//        String mHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.ServiceListData);

        Map<String, Object> map = new HashMap();
        map.put("limit", 10);
        map.put("page", page);
        map.put("flag", 1);
        NewAPIManager.getInstance().getEnterPrisenewAPI().trademarkLists(map).enqueue(new Callback<BaseEntity<ListResponse<List<IntellpropEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<IntellpropEntity>>>> call, Response<BaseEntity<ListResponse<List<IntellpropEntity>>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showTrademarkList(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<IntellpropEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 专利
     */
    public void getPatentList(int page) {
        Map<String, Object> map = new HashMap();
        map.put("limit", 10);
        map.put("page", page);
        map.put("flag", 1);
        DialogHelper.showProgressDialog(mFragment.getContext(), "加载中", 0, false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPatentList(map).enqueue(new Callback<BaseEntity<ListResponse<List<PatentEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PatentEntity>>>> call, Response<BaseEntity<ListResponse<List<PatentEntity>>>> response) {
                Log.d("TAG", "onResponse: 专利接口");
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showPatentList(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PatentEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 版权
     */
    public void getCopyrightList(int page) {
        Map<String, Object> map = new HashMap();
        map.put("limit", 10);
        map.put("page", page);
        map.put("flag", 1);
        DialogHelper.showProgressDialog(mFragment.getContext(), "加载中", 0, false);
        NewAPIManager.getInstance().getEnterPrisenewAPI().getCopyrightQueryList(map).enqueue(new Callback<BaseEntity<ListResponse<List<CopyrightListEntity2>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CopyrightListEntity2>>>> call, Response<BaseEntity<ListResponse<List<CopyrightListEntity2>>>> response) {
                Log.d("TAG", "onResponse: 版权接口");
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showCopyrightList(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CopyrightListEntity2>>>> call, Throwable t) {
                Log.d("TAG", "onFailure: " + call.toString());
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    /**
     * 公司
     *
     * @param limit
     */
    public void getCompanyList(int page, int limit, boolean isLoadMore) {
        pageCompany = isLoadMore ? pageCompany + 1 : 1;
        DialogHelper.showProgressDialog(mFragment.getContext(), "加载中", 0, false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getCompanyList(pageCompany, limit).enqueue(new Callback<BaseEntity<ListResponse<List<CompanyListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CompanyListEntity>>>> call, Response<BaseEntity<ListResponse<List<CompanyListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showCompanyList(response.body().getData().getList(), isLoadMore);

                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CompanyListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    /**
     * 资产
     *
     * @param limit
     */
    public void getPropertyList(int limit, boolean isLoadMore) {
        pageProperty = isLoadMore ? pageProperty + 1 : 1;
        DialogHelper.showProgressDialog(mFragment.getContext(), "加载中", 0, false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPropertyList(limit).enqueue(new Callback<BaseEntity<List<EpHotIpEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<EpHotIpEntity>>> call, Response<BaseEntity<List<EpHotIpEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    mFragment.showDealList(response.body().getData(), isLoadMore);
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<EpHotIpEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getOfferList(int page) {
        Map<String, Object> map = new HashMap();
        map.put("limit", 10);
        map.put("page", page);
        map.put("flag", 1);
        DialogHelper.showProgressDialog(mFragment.getContext(), "加载中", 0, false);
        NewAPIManager.getInstance().getEnterPrisenewAPI().getOfferList(map).enqueue(new Callback<BaseEntity<ListResponse<List<EpQuoteEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EpQuoteEntity>>>> call, Response<BaseEntity<ListResponse<List<EpQuoteEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showQuoteList(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EpQuoteEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //首页-专业团队列表
    public void getTeamList(int page) {
        Map<String, Object> map = new HashMap();
        map.put("limit", 10);
        map.put("page", page);
        map.put("flag", 1);
        DialogHelper.showProgressDialog(mFragment.getContext(), "加载中", 0, false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTeamQueryList(map).enqueue(new Callback<BaseEntity<ListResponse<List<TeamChangeEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TeamChangeEntity>>>> call, Response<BaseEntity<ListResponse<List<TeamChangeEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showTeamList(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TeamChangeEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();

            }
        });
    }

    // 需求大厅列表
    public void getDemandHallList(int page) {
        Map<String, Object> mMap = new HashMap();
        mMap.put("pageSize", page);
        mMap.put("limit", 10);
        mMap.put("flag", 1);
        DialogHelper.showProgressDialog(mFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getNeedAPI().getDemandHallList(mMap).enqueue(new Callback<BaseEntity<NeedEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<NeedEntity>> call, Response<BaseEntity<NeedEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.getDemandHallList(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<NeedEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mFragment.getActivity(), t.getMessage());
            }
        });
    }

}
